import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import { getToken } from '@/utils/auth'

export const routes = [
  {
    path: '/login',
    component: () => import('@/views/Login/index'),
    hidden: true
  },
  {
    path: 'cardAdd',
    component: () => import('@/views/Car/CarCard/addCard')
  },
  {
    path: '/exterpriseAdd',
    component: () => import('@/views/Park/Enterprise/addEnterprise')
  },
  {
    path: '/roleAdd',
    component: () => import('@/views/System/Role/AddRole.vue')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/workbench'
  },
  {
    path: '/workbench',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Workbench/index'),
      meta: { title: '工作台', icon: 'el-icon-data-board' }
    }]
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }

]
// export const routes = [
//   {
//     path: '/login',
//     component: () => import('@/views/Login/index'),
//     hidden: true
//   },
//   {
//     path: '/addCard',
//     component: () => import('@/views/Car/CarCard/add-card'),
//     hidden: true
//   },
//   {
//     path: '/addEnterprise',
//     component: () => import('@/views/Park/Enterprise/AddEnterprise'),
//     hidden: true
//   },
//   {
//     path: '/enterpriseDetail',
//     component: () => import('@/views/Park/Enterprise/EnterpriseDetail'),
//     hidden: true
//   },
//   {
//     path: '/addRole',
//     component: () => import('@/views/System/Role/AddRole'),
//     hidden: true
//   },
//   {
//     path: '/',
//     component: Layout,
//     redirect: '/workbench'
//   },
//   // 只有一级路由
//   // 一级路由：负责把layout架子渲染出来
//   // 二级路由：path为空 会作为默认的二级路由一上来就渲染出来
//   {
//     path: '/workbench',
//     component: Layout,
//     children: [{
//       path: '',
//       component: () => import('@/views/Workbench/index'),
//       meta: { title: '工作台', icon: 'el-icon-data-board' }
//     }]
//   },

//   {
//     path: '/404',
//     component: () => import('@/views/404'),
//     hidden: true
//   }
// ]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: routes
})

const router = createRouter()
const WHITE_LIST = ['/login', '/404']
router.beforeEach((to, from, next) => {
  const token = getToken()

  if (token) {
    next()
  } else {
    // console.log(token)
    if (WHITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
// 重置路由方法
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter()
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher
}

export default router
